import axios from "axios";
import type {
  AxiosInstance,
  AxiosError,
  InternalAxiosRequestConfig,
  AxiosResponse,
} from "axios";

const baseURL = import.meta.env.VITE_API_BASE_URL;

export const authHeader = localStorage.token
  ? "Bearer " + localStorage.token
  : "";

export const server: AxiosInstance = axios.create({
  baseURL: baseURL,
  timeout: 50000,
});
console.log(baseURL);

server.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    return config;
  },
  (error: AxiosError) => {
    return Promise.reject(error);
  }
);

// 响应拦截
server.interceptors.response.use(
  (response: AxiosResponse<any, any>) => {
    const { status, data } = response;
    if (status === 200) {
      return data;
    }
  },
  (error: AxiosError) => {
    return Promise.reject(error);
  }
);

export function batchLogin() {
  return server({
    url: "/action?method=login",
    method: "post",
    data: {
      acc: "123456789",
      pwd: "159357",
      recover: "asdfgh",
      name: "147258",
    },
  });
}

export function register() {
  return server({
    url: "/action?method=adminlogin",
    method: "post",
    data: {
      username: "xl",
      password: "123456",
    },
  });
}
